#include <iostream>
#include <SDL/SDL.h>

#include "sprite.h"
#include "explosao.h"

/******************************************************************************
    Construtor(pos)
    Inicializa os elementos da matriz.
******************************************************************************/
Explosao::Explosao( int altura, int matriz[10]) {
    for (int i = 0; i < 10; i++) {
        //Cria pecas
        pecas.push_back(
            new Peca_Explosao(matriz[i],
                (rand() % 10) * -0.02,  //Vy0
                -0.1 + (0.02 * (rand() % 10) ),// Vx0
                (i*20) + 5,
                (altura*20)
            )
        );
    }
}

/******************************************************************************
    Construtor(pos)
    Inicializa os elementos da matriz.
******************************************************************************/
Explosao::Explosao(int x, int altura, int matriz[10]) {
    for (int i = 0; i < 10; i++) {
        //Cria pecas
        pecas.push_back(
            new Peca_Explosao(matriz[i],
                (rand() % 10) * -0.02,  //Vy0
                -0.1 + (0.02 * (rand() % 10) ),// Vx0
                x + (i*20) + 5,
                (altura*20)
            )
        );
    }
}

/******************************************************************************
    Atualizar(dt)
    Atualiza a posicao das pecas
******************************************************************************/
void Explosao::Atualizar( Uint32 dt) {
    list<Peca_Explosao*>::iterator it;
    for (it = pecas.begin(); it != pecas.end(); it++) {
        (*it)->Atualizar(dt);        
    }

    for (it = pecas.begin(); it != pecas.end(); ) {
        if ( (*it)->x > 210 || (*it)->x < 0 || (*it)->y > 500) {
            delete((*it));
            pecas.erase(it);
            it = pecas.begin();
        } else {
            it++;
        }
    }
}

/******************************************************************************
    AtualizarSplash(dt)
    Atualiza a posicao das pecas
******************************************************************************/
void Explosao::AtualizarSplash( Uint32 dt) {
    list<Peca_Explosao*>::iterator it;
    for (it = pecas.begin(); it != pecas.end(); it++) {
        (*it)->Atualizar(dt);
    }

    for (it = pecas.begin(); it != pecas.end(); ) {
        if ( (*it)->x < 0 || (*it)->y > 600) {
            delete((*it));
            pecas.erase(it);
            it = pecas.begin();
        } else {
            it++;
        }
    }
}

/******************************************************************************
    Draw(tela)
    Faz as pecas se 'auto desenharem'
******************************************************************************/
void Explosao::Draw( SDL_Surface *tela) {
     //cout << "\t\t Desenhando pecas \n"; cout.flush();
    list<Peca_Explosao*>::iterator it;
    for (it = pecas.begin(); it != pecas.end(); it++) {
        //cout << "Proximo ->";cout.flush();
        (*it)->Draw(tela);
    }
         //cout << "\t\t OK \n"; cout.flush();

}

/******************************************************************************
    Destrutor()
******************************************************************************/
Explosao::~Explosao(void) {
    pecas.clear();
}
